<?
include_once('conn.php');
include_once('include/func.php');
include("admin_check.php") ;
if($admin[0][2]<>10 and $admin[0][2]<>9){exit('<script>alert("您没有权限"); window.history.back(-1);</script>');}
///

if(isset($_GET[orderby])){
	setcookie("orderby", $_GET[orderby]);
	echo $current_url = 'HTTP://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	header("Location: $nowpage");
	//header("Location: $current_url");
	
	}
if(isset($_GET[s_text])){
		if($_GET[s_type]=='PRODUCT_NAME'){
		$is_type_null=$_GET[s_text]==null?'':' and `name` LIKE \'%'.$_GET[s_text].'%\'';
		$is_type_null_list=$_GET[s_text]==null?'':' and a.name LIKE \'%'.$_GET[s_text].'%\'';
		$is_type_null_c=$_GET[s_text]==null?'':' where `name` LIKE \'%'.$_GET[s_text].'%\'';
		}elseif($_GET[s_type]=='PRODUCT_MODEL'){
			$model = $db->select("SELECT a.sid FROM `".tablepre."stock_attachment` a left join `".tablepre."stock` b on a.sid = b.sid where b.delete = 0 and a.type = 'model' and a.value1 like '%".$_GET[s_text]."%'");
			if(count($model)>0){
			for($i=0;$i<count($model);$i++){
				$modelarr .= $model[$i][0].',';
				}
				$modelarr_e = array_filter(explode(',',$modelarr));
				//echo count($modelarr_e);
				$modelarr_i = implode(',',$modelarr_e);
				$is_type_null=$_GET[s_text]==null?'':' and `sid` in ('.$modelarr_i.')';
				$is_type_null_list=$_GET[s_text]==null?'':' and a.sid in ('.$modelarr_i.')';
				$is_type_null_c=$_GET[s_text]==null?'':' where `sid` in ('.$modelarr_i.')';
			}else{exit('<script>alert("没有符合条件商品"); window.history.back(-1);</script>');}
			}elseif($_GET[s_type]=='PRODUCT_BATCH_NO'){
			$batch = $db->select("SELECT a.sid FROM `".tablepre."stock_item` a left join `".tablepre."stock` b on a.sid = b.sid where b.onsale = 1 and b.delete = 0 and a.batch like '%".$_GET[s_text]."%'");
			if(count($batch)>0){
			for($i=0;$i<count($batch);$i++){
				$batcharr .= $batch[$i][0].',';
				}
				$batcharr_e = array_filter(explode(',',$batcharr));
				//echo count($modelarr_e);
				$batcharr_i = implode(',',$batcharr_e);
				$is_type_null=$_GET[s_text]==null?'':' and `sid` in ('.$batcharr_i.')';
				$is_type_null_list=$_GET[s_text]==null?'':' and a.sid in ('.$batcharr_i.')';
				$is_type_null_c=$_GET[s_text]==null?'':' where `sid` in ('.$batcharr_i.')';
			}else{exit('<script>alert("没有符合条件商品"); window.history.back(-1);</script>');}
			}
	
	}
	elseif(isset($_GET[stock_item])){
		$item = $db->select("SELECT a.sid FROM `".tablepre."stock_item` a left join `".tablepre."stock` b on b.sid = a.sid where  b.onsale = 1 and a.quantity < '".parameters(20)."'");
		for($i=0;$i<count($item);$i++){
			$item_arr .= $item[$i][0].',';
			}
			$item_arr_e = array_filter(explode(',',$item_arr));
			$item_arr_i = implode(',',$item_arr_e);
			$arr_sid_item=' and a.sid in ('.$item_arr_i.')';
			$is_type_null_c=' where `sid` in ('.$item_arr_i.')';
		}
	else{
if($_GET[tid]<>null){
	
	function arr_father($con){
	global $db; global $tablepre;
	$arr_type_list = $db->select("SELECT tid,father FROM `".tablepre."stock_type` where father = $con");
	for($i=0;$i<count($arr_type_list);$i++){
		$arr .= $arr_type_list[$i][0].',';
		//arr_father($con=$arr_type_list[$i][0]);
		}
		return $arr;
	}
	
$arr_type_list = $db->select("SELECT tid,father FROM `".tablepre."stock_type` where father = ".$_GET[tid]."");
if($arr_type_list[0][0]==null){
	$tid_array=$_GET[tid];
	}else{
for($i=0;$i<count($arr_type_list);$i++){
	
	$tid_array .= arr_father($arr_type_list[$i][0]).','.$arr_type_list[$i][0].',';
	
	}
$tid_array1 = array_filter(explode(',',$tid_array));
$tid_array = implode(',',$tid_array1);
	}
$arr_type_list = $db->select("SELECT tid,father FROM `".tablepre."stock_type` where father in (".$tid_array.")");

if($arr_type_list[0][0]==null){$tid_array4=$tid_array; }else{
for($i=0;$i<count($arr_type_list);$i++){
	$tid_array2 .= $arr_type_list[$i][0].',';
	}
$tid_array3 = array_filter(explode(',',$tid_array2));

$tid_array5 = array_merge($tid_array1,$tid_array3);
$tid_array4 = implode(',',$tid_array3);
}


	}
	
	
}
///
$e_tid = explode(',',$tid_array4);
foreach($e_tid as $v){
	$to_catalogue = $db->select("SELECT products_id FROM `".tablepre."to_categories` WHERE `categories_id` = '".$v."'");
	if(count($to_catalogue)>0){
		for($i=0;$i<count($to_catalogue);$i++){
			$arr.= $to_catalogue[$i][0].',';
			}

		}
	}
				$pid_array=array_filter(explode(',',$arr));
				if($pid_array==null){
					$pid_array2='';
					$pid_array3='';
					}elseif($_GET[tid]==null){
						$pid_array2='';
						$pid_array3='';
						}else{
							$pid_array2='OR sid in('.implode(',',$pid_array).')';
							$pid_array3='OR a.sid in('.implode(',',$pid_array).')';
							}

	if($_GET[s_type]==null) $is_type_null=$_GET[tid]==null?'':'where tid in ('.$tid_array4.')';
	
	//echo $is_type_null_c;
$newSql="select count(sid) as count from `".tablepre."stock` ".$is_type_null_c.$is_type_null." ".$pid_array2."";//获取表中的总记录数 
@$resultCount=mysql_query($newSql,$dbh);// or die("执行SQL语句失败!");//返回结果集
@$reArray=mysql_fetch_assoc($resultCount);//从结果集中取出一行,作为相关数组处理 
//print_r($resultCount);
$amount=$reArray['count'];//得到总记录数 
$pageSize=48;//每页显示5条记录  24
$pageamount=floor($pageamount=$amount/$pageSize); //总页数
//////////////////////分类页数
$offset=isset($_GET[page])?($_GET[page]-0)*$pageSize:0;
include('include/bpage.php'); 
//分页
?>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? echo systemname;?></title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
	<!-- jquery packed -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="js/dtree.js"></script>
	<!-- tableRowCheckboxToggle -->
<script src="js/jc_table.js" type="text/javascript"></script>
    <script type="text/javascript"> 
    function fade() 
{ 
    //停留2秒后消失,在停留的时候,透明度为1.0什么都不做 
    $('<div id="add" class="quick-alert">操作成功</div>').insertAfter($("#example")).animate({opacity: 1.0}, 2000).fadeOut("slow",function(){ 
   //隐藏时把元素删除 
   $(this).remove(); 
}); 
} 
</script> 
<script type="text/javascript" src="js/jquery_ajax_order.js"></script>
<?

if($_POST[ch]==1){
	foreach ($_POST[selectid] as $value) {
		$updatedate = $db->UPDATE("UPDATE `".tablepre."stock` SET `onsale`='0' WHERE (`sid`='$value')");
	}
				header("Location: $nowpage?ok");
	exit;	
	}
if($_POST[ch]==2){
	foreach ($_POST[selectid] as $value) {
		$updatedate = $db->UPDATE("UPDATE `".tablepre."stock` SET `onsale`='1' WHERE (`sid`='$value')");
	}
				header("Location: $nowpage?ok");
	exit;	
	}
if($_POST[ch]==3){
	foreach ($_POST[selectid] as $value) {
		$updatedate = $db->UPDATE("UPDATE `".tablepre."stock` SET `delete`='1' WHERE (`sid`='$value')");
	}
				header("Location: $nowpage?ok");
	exit;	
	}
?>
<link href="js/facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="js/facefiles/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox() 
    })
</script>
</head>
<body <? echo isset($_GET[ok])?'onload="fade();"':''; ?>>
<div id="wrap">
<div id="main" class="clearfix">
<div id="side">
  </div>
  <div id="head"><? include_once('admin_head.php');?></div>
  <table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td width="13%" valign="top"><div id="left"><?
  include_once('admin_menu.php');
  ?></div></td>
    <td width="87%" valign="top"><div id="right">
<div id="example"> </div><table width="1050" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td valign="top"><form id="form2" name="form2" method="post" action="">





<table border="0" cellspacing="1" cellpadding="0" class="jc_table">

  <tr class="header">
    <td>选择</td>
    <td>名称</td>
    <td>型号</td>
    <td>库存</td>
    <td align="center">当前库存<br />
     <div style=" width:55px;">  <a href="?orderby=c_down"><img src="images/icon_5<? echo $_COOKIE[orderby]=='c_down'?'_2':'';?>.gif" style="float:left;" /></a><a href="?orderby="><img src="images/icon_7.gif" style="float:left;" /></a><a href="?orderby=c_up"><img src="images/icon_6<? echo $_COOKIE[orderby]=='c_up'?'_2':'';?>.gif" style="float:left;" /></a></div></td>
    <? if(parameters(25)>0){?><td align="center">合理库存</td><? }?>
    <? if(parameters(26)==1){?><td align="center">应补货</td><? }?>
    <td align="center">起订量</td>
    <td align="center">上月销售<br />
    </td>
    <td align="center">状态<br />
     <div style=" width:55px;"><a href="?orderby=onsaledown"><img src="images/icon_5<? echo $_COOKIE[orderby]=='onsaledown'?'_2':'';?>.gif" style="float:left;" /></a><a href="?orderby="><img src="images/icon_7.gif" style="float:left;" /></a><a href="?orderby=onsaleup"><img src="images/icon_6<? echo $_COOKIE[orderby]=='onsaleup'?'_2':'';?>.gif" style="float:left;" /></a></div></td>
    <td>修改</td>
  </tr>


<style>.thisstyle{ width:910px;}</style>
<?
function restock($sid){
	global $db; global $tablepre;
	$list = $db->select("SELECT value FROM `".tablepre."restock` where sid = '".$sid."' limit 1");
	return $list[0][0];
	}

	
//echo $is_type_null.$pid_array2;
eval("\$shop = ".parameters(1));
function module_shop($shop_arr,$stock_id,$type_id,$father_id){
	global $db; global $tablepre;
			foreach($shop_arr as $value){
				$is_module = $db->select("SELECT active FROM `".tablepre."module` where `name` = '$value'");
	  if($is_module[0][0]==1){return '<img src="images/icon_4.gif" alt="已与'.$value.'同步" />';}else{
		 return '<a href="admin_stock_add.php?getch&sid='.$type_id.'&tid='.$father_id.'&nowid='.$stock_id.'"><img src="images/icon_1.gif" alt="修改" /></a></a>';
		  }
		}
	}

///////检查插件
echo nav();
?>
<div style="float:right; margin:10px 0 0 0;">
<?
foreach($_GET as $k => $v){
	if($k<>'page'){
		$url .= $k.'='.$v.'&';
		
		}		
	}
?><script>
function page2(pageamount){
	var pageamount = parseInt(pageamount)+1;
	var value=document.getElementById("page").value;
	var gotopage;
	if(value == null){
		gotopage = 0;
		}else if(value>=pageamount){
			gotopage = pageamount;
			
			}else{
				gotopage = value;		
				}
	document.getElementById("page").value = gotopage;
	}
function page_go_to(){
	var value=parseInt(document.getElementById("page").value)-1;
	location='?<? echo $url; ?>&page='+value;
	}
</script>

跳转到 <input type="text" value="<? echo $_GET[page]+1;?>" size="1" id="page" onkeyup="page2('<? echo $pageamount; ?>')" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" />  <input type="button" value="页" onclick="page_go_to()" /></div>
<?
echo $pages;
if(isset($_GET[stock_item])){$is_type_null_list=$arr_sid_item;}else{
	if(!isset($_GET[s_text])){$is_type_null_list=$_GET[tid]==null?'':'and a.tid in ('.$tid_array4.')';}
	}
	if($_COOKIE[orderby]==null){
		$sql_order_by='group by b.sid order by a.sid desc';
		}elseif($_COOKIE[orderby]=='onsaledown'){
			$sql_order_by='group by b.sid order by a.onsale desc';
			}elseif($_COOKIE[orderby]=='onsaleup'){
				$sql_order_by='group by b.sid order by a.onsale ';
				}elseif($_COOKIE[orderby]=='c_down'){
					$sql_order_by='group by b.sid order by sum(b.quantity) ';
					}elseif($_COOKIE[orderby]=='c_up'){
						$sql_order_by='group by b.sid order by sum(b.quantity) desc';
						}
$arr_list = $db->select("SELECT a.sid,a.name,a.tid,a.onsale FROM `".tablepre."stock` a left join `".tablepre."stock_item` b on(a.sid = b.sid) where a.delete = '0' ".$is_type_null_list." ".$pid_array3." ".$sql_order_by." limit $offset,$pageSize");
for($i=0;$i<count($arr_list);$i++) {
	$t_row=$i%2==0?1:2;
	$status = $arr_list[$i][3]==0?'<img src="images/icon_2.gif" alt="已下架" />':'<img src="images/icon_3.gif" alt="出售中" />';
	if(parameters(25)>0){
		$last_m_s = previous_month_sale($arr_list[$i][0])==0?current_item_ctock($arr_list[$i][0]):previous_month_sale($arr_list[$i][0])*parameters(25);
		$last_m_s_code ='<td align="center"><a title="'.$last_m_s.' '.parameters(24).'">'.$last_m_s.' '.parameters(24).'</a></td>';
		}
	if(parameters(26)==1){
		$restock_code = '<td><div id="loading'.$arr_list[$i][0].'" style=" display:none;"></div><input type="text" size="1" id="restock'.$arr_list[$i][0].'" onkeyup="restock('.$arr_list[$i][0].')" value="'.restock($arr_list[$i][0]).'" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"/></td>';
		}
	
 echo '<tr class="t'.$t_row.'">
<td><input type="checkbox" id="checkbox" name="selectid[]" value="'.$arr_list[$i][0].'" /></td>
<td>'.$arr_list[$i][1].'</td>
<td>'.attachment($con0=1,$con1=$arr_list[$i][0],$con2=model).'</td>
<td><a href="admin_item.php?iframe&sid='.$arr_list[$i][0].'" rel="facebox[.thisstyle]"><img src="images/icon_1.gif" alt="编辑" /></a></td>
<td align="center">'.current_item_ctock($arr_list[$i][0]).' '.parameters(24).'</td>
'.$last_m_s_code.$restock_code.'
<td><div id="loading2_'.$arr_list[$i][0].'" style=" display:none;"></div><input type="text" size="1" id="minimun'.$arr_list[$i][0].'" onkeyup="minimun('.$arr_list[$i][0].')" value="'.minimun($arr_list[$i][0]).'" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"/></td>
<td align="center">'.previous_month_sale($arr_list[$i][0]).' '.parameters(24).'</td>
<td align="center">'.$status.'</td>
<td>'.module_shop($shop,$arr_list[$i][0],$arr_list[$i][2],prefather_id($arr_list[$i][2])).'</td>
</tr>
';
}

?>
  <tr>
    <td colspan="6"><label style="float:left; margin:3px 0 0 0;">全选</label><input type="checkbox" id="all"/></td>
    </tr>
</table><div id="clear"></div>
<? echo $pages;?>
<select name="ch">
  <option value="1">下架</option>
  <option value="2">上架</option>
  <option value="3">删除</option>
</select>
<input type="submit" name="button2" id="button2" value="提交" />
</form></td>
    <td valign="top">
    <style>
	#menu {
		border:solid #CCC 1px; padding:5px;}
    #menu ul{
		margin:0px; padding:0px;}
	#menu li{
		list-style-type:none;}
		#f{
			font-size:14px; font-weight:bold;}
			
			/**/
			#page{}
#page ul{
	margin: 0px;
	padding: 0px;
}
#page .page2{
	margin: 3px;
	padding: 3px;
	float: left;
	border: 1px solid #FFF;
	list-style-type: none;
}
#page ol{
	margin: 3px;
	float: left;
	border: 1px solid #C03434;
	list-style-type: none;
	padding-top: 3px;
	padding-right: 5px;
	padding-bottom: 3px;
	padding-left: 5px;
}
#page dl{
	margin: 3px;
	float: left;
	border: 1px solid #C03434;
	list-style-type: none;
	padding-top: 3px;
	padding-right: 5px;
	padding-bottom: 3px;
	padding-left: 5px;
	background-color: #FFDFDF;
	font-weight: bold;
}
    </style>
    <div id="menu"><div style=" background:#333; color:#FFF; font-weight:bold; padding:2px;">分类</div>
<ul>
<?

$arr_list = $db->select("SELECT * FROM `".tablepre."stock_type` order by rank");
      function fenlei_list($f_id=0,$lever=1)   
{   
    global $arr_list;   
    if($f_id == 0)    
        $lever=1;   
    else   
        $lever +=1;   
    for($i=0;$i<count($arr_list);$i++)   
    {   
        if($arr_list[$i][3]==$f_id)   
        {   
		if($arr_list[$i][3]==0){$classcolor='odd_row';}elseif($arr_list[$i][3]==1){$classcolor='st1_row';}elseif($arr_list[$i][3]==2){$classcolor='st2_row';}elseif($arr_list[$i][3]==3){$classcolor='st3_row';}elseif($arr_list[$i][3]==4){$classcolor='st4_row';}else{$classcolor='even_row';}
		//$classcolor=$arr_list[$i][3]==0?'odd_row':'even_row';
		$code=$lever==1?'id="f"':''; $code2=$lever<>1?'●':'';
			echo '<li '.$code.'>'.@str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$lever-2).$code2.'<a href="?tid='.$arr_list[$i][0].'">'.$arr_list[$i][1].'
			</a></li>';
            fenlei_list($arr_list[$i][0],$lever);   
        }      
    }      
}  

			echo fenlei_list();
			
?>
</ul>
</div></td>
  </tr>
</table>

</div></td>
  </tr>
</table>

  
  
  </div>
</div>

<div id="footer">
<?
include_once('fooder.php');
?>
</div>
</BODY>
</HTML>
